
package keul.planner.tools;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import keul.planner.entities.Station;
import keul.planner.entities.Line;
import keul.planner.entities.City;

/**
 * Hier werden die Daten gehalten, damit das Projekt auch ohne 
 * Datenbanktabellen gestartet werden kann. Es können mithilfe
 * dieser Klasse diese Tabellen gebildetet werden.
 * @author kev
 */
public class TestData {
    
    public static City getCity(EntityManager em, String name) {
        return em.createQuery("SELECT c FROM City c WHERE c.name=\"" + name + '\"', City.class).getSingleResult();
    }
    
    public static void generateStations(EntityManager em) {

        // STATIONEN / NETZ //
        
        Station emden_ausse = new Station(getCity(em, "Emden, Ostfriesland"), "Emden-Außenhafen", 53.26f, 7.02f);
        Station emden       = new Station(getCity(em, "Emden, Ostfriesland"), "Emden-Hauptbahnhof", 53.23f, 7.2167f);
        Station norddeich   = new Station(getCity(em, "Norden, Ostfriesland"), "Norddeich-Mole");
        Station leer        = new Station(getCity(em, "Leer (Ostfriesland)"));
        //EMD -> HB
        Station oldenburg   = new Station(getCity(em, "Oldenburg in Oldenburg"));
        Station delmenhorst = new Station(getCity(em, "Delmenhorst"));
        Station bremen      = new Station(getCity(em, "Bremen"));
        // HB -> HH
        Station hh_harburg  = new Station(getCity(em, "Hamburg"), "Hamburg-Harburg", 53.28f, 9.951f);
        Station hamburg     = new Station(getCity(em, "Hamburg"), "Hamburg-Hauptbahnhof", 53.32f, 10.0f);
        Station hh_dammtor  = new Station(getCity(em, "Hamburg"), "Hamburg-Dammtor", 53.42f, 10.02f);
        Station hh_altona   = new Station(getCity(em, "Hamburg"), "Hamburg-Altona", 53.52f, 10.03f);
        // HH -> B
        Station buechen     = new Station(getCity(em, "Büchen, Lauenburg"));
        Station ludwigslust = new Station(getCity(em, "Ludwigslust, Mecklenburg"));
        Station wittenberge = new Station(getCity(em, "Wittenberge, Prignitz"));
        Station b_spandau   = new Station(getCity(em, "Berlin"), "Berlin-Spandau", 52.32f, 13.11f);
        Station berlin      = new Station(getCity(em, "Berlin"), "Berlin-Hauptbahnhof", 52.31f, 13.21f);
        // HH -> KI
        Station neumuenster = new Station(getCity(em, "Neumünster, Holstein"));
        Station kiel        = new Station(getCity(em, "Kiel"));
        
        //  B -> H
        Station rathenow    = new Station(getCity(em, "Rathenow"));
        Station stendal     = new Station(getCity(em, "Stendal"));
        Station wolfsburg   = new Station(getCity(em, "Wolfsburg"));
        Station hannover    = new Station(getCity(em, "Hannover"));
        //  H -> HH
        Station celle       = new Station(getCity(em, "Celle"));
        Station uelzen      = new Station(getCity(em, "Uelzen, Lüneburger Heide"));
        Station lueneburg   = new Station(getCity(em, "Lüneburg"));
        //  H -> HB
        Station nienburg    = new Station(getCity(em, "Nienburg (Weser)"));
        Station verden      = new Station(getCity(em, "Verden (Aller)"));
        
        Station osnabrueck  = new Station(getCity(em, "Osnabrück"));
        Station rheine      = new Station(getCity(em, "Rheine"));
        //-->EMD
        Station lingen      = new Station(getCity(em, "Lingen (Ems)"));
        Station meppen      = new Station(getCity(em, "Meppen"));
        Station papenburg   = new Station(getCity(em, "Papenburg"));
        
        
        
        emden_ausse.setNeighbors(   new Station[]{emden} );                         //
        emden.setNeighbors(         new Station[]{emden_ausse, leer, norddeich} );  //////
        norddeich.setNeighbors(     new Station[]{emden} );                         //
        leer.setNeighbors(          new Station[]{emden, oldenburg, papenburg} );   //////
        
        oldenburg.setNeighbors(     new Station[]{leer, delmenhorst} );             ////
        delmenhorst.setNeighbors(   new Station[]{oldenburg, bremen} );             ////
        bremen.setNeighbors(        new Station[]{delmenhorst, hh_harburg, verden} );/////
        
        hh_harburg.setNeighbors(    new Station[]{bremen, hamburg, lueneburg} );    ////////
        hamburg.setNeighbors(       new Station[]{hh_harburg, hh_dammtor, buechen} );///
        hh_dammtor.setNeighbors(    new Station[]{hamburg, hh_altona, neumuenster} );///
        hh_altona.setNeighbors(     new Station[]{hh_dammtor} );                    //

        buechen.setNeighbors(       new Station[]{hamburg, ludwigslust} );          ////
        ludwigslust.setNeighbors(   new Station[]{buechen, wittenberge} );          ////
        wittenberge.setNeighbors(   new Station[]{ludwigslust, stendal, b_spandau} );/////
        b_spandau.setNeighbors(     new Station[]{rathenow, berlin, wittenberge} );  //////
        berlin.setNeighbors(        new Station[]{b_spandau} );                     //
        
        neumuenster.setNeighbors(   new Station[]{hh_dammtor, kiel} );              ////
        kiel.setNeighbors(          new Station[]{neumuenster} );                     //
                
        rathenow.setNeighbors(      new Station[]{b_spandau, stendal} );            ////
        stendal.setNeighbors(       new Station[]{rathenow, wolfsburg, wittenberge} );////
        wolfsburg.setNeighbors(     new Station[]{stendal, hannover} );             ////
        hannover.setNeighbors(      new Station[]{wolfsburg, celle} );              ////
        
        celle.setNeighbors(         new Station[]{hannover, uelzen} );              ////
        uelzen.setNeighbors(        new Station[]{celle, lueneburg} );              ////
        lueneburg.setNeighbors(     new Station[]{uelzen, hh_harburg} );            //       
        
        nienburg.setNeighbors(      new Station[]{hannover, verden} );              ////
        verden.setNeighbors(        new Station[]{nienburg, bremen} );              ////
        
        osnabrueck.setNeighbors(    new Station[]{bremen, rheine} );                ////
        rheine.setNeighbors(        new Station[]{osnabrueck, lingen} );            ////
        
        lingen.setNeighbors(        new Station[]{rheine, meppen} );                ////
        meppen.setNeighbors(        new Station[]{lingen, papenburg} );             ////
        papenburg.setNeighbors(     new Station[]{meppen, leer} );                  ////
        
        
        
        em.persist(emden_ausse);    em.persist(emden);          em.persist(norddeich);
        em.persist(leer);           em.persist(oldenburg);      em.persist(delmenhorst);
        em.persist(bremen);         em.persist(hh_harburg);     em.persist(hamburg);
        em.persist(hh_dammtor);     em.persist(hh_altona);      em.persist(buechen);
        em.persist(ludwigslust);    em.persist(wittenberge);    em.persist(b_spandau);
        em.persist(berlin);         em.persist(neumuenster);    em.persist(kiel);
        em.persist(rathenow);       em.persist(stendal);
        em.persist(wolfsburg);      em.persist(hannover);       em.persist(celle);
        em.persist(uelzen);         em.persist(lueneburg);      em.persist(nienburg);
        em.persist(verden);         em.persist(osnabrueck);     em.persist(rheine);
        em.persist(lingen);         em.persist(meppen);         em.persist(papenburg);

        
        // ROUTEN //
        Line line1 = new Line(); // Emden-Außenhafen - Kiel
        line1.setName("Linie 1 (Emden-Außenhafen - Kiel)");
        line1.setStops(new Station[]{
            emden_ausse, emden, leer, oldenburg, delmenhorst, bremen,
            hh_harburg, hamburg, hh_dammtor, neumuenster, kiel
        });
        line1.setDeparture(generateTimeTable(30, 100, 1300));     
        line1.setAverageVelocity(90);
        em.persist(line1);
        Line line1R = new Line(); // Kiel - Emden-Außenhafen
        line1R.setName("Linie 1 (Kiel - Emden-Außenhafen)");
        List<Station> stops = new ArrayList<Station>();
        for (Station station : line1.getStops()) 
            stops.add(0, station);
        line1R.setStops( stops );
        line1R.setDeparture(generateTimeTable(30, 100, 1300));     
        line1R.setAverageVelocity(90);
        em.persist(line1R);
        
        
        Line line2 = new Line(); // Norddeich-Mole - Hamburg
        line2.setName("Linie 2 (Norddeich-Mole (-Osnabrück-) Hamburg)");
        line2.setStops(new Station[]{
            norddeich, emden, leer, papenburg, meppen, lingen,
            rheine, osnabrueck, bremen, verden, nienburg, hannover,
            celle, uelzen, lueneburg, hh_harburg, hamburg
        });
        line2.setDeparture(generateTimeTable(60, 120, 1250));     
        line2.setAverageVelocity(80);
        em.persist(line2);          
        Line line2R = new Line();
        line2R.setName("Linie 2 (Hamburg (-Osnabrück-) Norddeich-Mole)");
        stops = new ArrayList<Station>();
        for (Station station : line2.getStops()) 
            stops.add(0, station);
        line2R.setStops( stops );
        line2R.setDeparture(generateTimeTable(60, 100, 1300));     
        line2R.setAverageVelocity(80);
        em.persist(line2R);
        
        
        Line line3 = new Line(); // Hannover - Berlin
        line3.setName("Linie 3 (Hannover - Berlin)");
        line3.setStops(new Station[]{
            hannover, wolfsburg, stendal, rathenow, b_spandau, berlin
        });
        line3.setDeparture(generateTimeTable(30, 100, 1400));       
        line3.setAverageVelocity(110);
        em.persist(line3);
        Line line3R = new Line();
        line3R.setName("Linie 3 (Berlin - Hannover)");
        stops = new ArrayList<Station>();
        for (Station station : line3.getStops()) 
            stops.add(0, station);
        line3R.setStops( stops );
        line3R.setDeparture(generateTimeTable(30, 100, 1300));     
        line3R.setAverageVelocity(110);
        em.persist(line3R);        
     
        
        Line line4 = new Line(); // Berlin - Kiel
        line4.setName("Linie 4 (Berlin - Kiel)");
        line4.setStops(new Station[]{
            berlin, b_spandau, wittenberge, ludwigslust, buechen, hamburg,
            hh_dammtor, neumuenster, kiel
        });
        line4.setDeparture(generateTimeTable(40, 200, 1200));       
        line4.setAverageVelocity(120);
        em.persist(line4);    
        Line line4R = new Line();
        line4R.setName("Linie 4 (Kiel - Berlin)");
        stops = new ArrayList<Station>();
        for (Station station : line4.getStops()) 
            stops.add(0, station);
        line4R.setStops( stops );
        line4R.setDeparture(generateTimeTable(40, 100, 1300));     
        line4R.setAverageVelocity(120);
        em.persist(line4R); 
        
        
        Line line5 = new Line(); // Berlin - Rheine - Emden
        line5.setName("Linie 5 (Berlin - Rheine - Emden)");
        line5.setStops(new Station[]{
            berlin, b_spandau, rathenow, stendal, wolfsburg, hannover,
            nienburg, verden, bremen, osnabrueck, rheine, lingen,
            meppen, papenburg, leer, emden
        });
        line5.setDeparture(generateTimeTable(30, 200, 1200));       
        line5.setAverageVelocity(120);
        em.persist(line5);          
        Line line5R = new Line();
        line5R.setName("Linie 5 (Emden - Rheine - Berlin)");
        stops = new ArrayList<Station>();
        for (Station station : line5.getStops()) 
            stops.add(0, station);
        line5R.setStops( stops );
        line5R.setDeparture(generateTimeTable(30, 100, 1300));     
        line5R.setAverageVelocity(120);
        em.persist(line5R); 
        

    }

    public static void deleteStationsAndLines(EntityManager em) {
        List<Line> lines = (em.createQuery("select l from Line l", Line.class)).getResultList();
        for (Line line : lines) {
            em.remove(line);
        }
        List<Station> stations = (em.createQuery("select s from Station s", Station.class)).getResultList();
        for (Station station : stations) {
            em.remove(station);
        }
    }
    
    public static int[] generateTimeTable() {
        return generateTimeTable(60, 0, 1439);
    }
    public static int[] generateTimeTable(int interval, int start, int end) {
        int[] departures = new int[(end-start)/interval+1];
        int index = 0 ;
        for (int i = start; i <= end; i+=interval) {
            departures[index++]=i;
        }
        return departures;
    }
    
    public static Station getStation(EntityManager em, String name) {
        return em.createQuery("SELECT s FROM Station s WHERE s.name=\"" + name + '\"', Station.class).getSingleResult();
    }


}
